package com.maserb.monitor.service.jwQuestion.impl;

import com.maserb.monitor.entity.jwQuestion.JwQuestionUserEntity;
import com.maserb.monitor.repository.JwQuestion.JwQuestionUserRepository;
import com.maserb.monitor.service.base.impl.JpaSpecificationServiceImpl;
import com.maserb.monitor.service.jwQuestion.JwQuestionUserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class JwQuestionUserServiceImpl extends JpaSpecificationServiceImpl<JwQuestionUserRepository,
        JwQuestionUserRepository, JwQuestionUserEntity, String>
        implements JwQuestionUserService {

    @Override
    public List<JwQuestionUserEntity> findByUserName(String userId) {
        return (this.repository.findByUserName(userId));
    }

    @Override
    public List<JwQuestionUserEntity> findByQuestionIdAndUserName(String questionId, String userId) {
        return this.repository.findByQuestionIdAndUserName(questionId, userId);
    }

    @Override
    public List<JwQuestionUserEntity> findAllByQuestionIdOrderByCreateTimeAsc(String questionId) {
        return this.repository.findAllByQuestionIdOrderByCreateTimeAsc(questionId);
    }

    @Override
    public Page<JwQuestionUserEntity> findAllByUserName(Pageable pageable, String userName) {
        return this.findAll(new Specification<JwQuestionUserEntity>() {
            @Override
            public Predicate toPredicate(Root<JwQuestionUserEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> pathUserName = root.get("userName");

                ArrayList<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmptyOrWhitespace(userName)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathUserName, userName)));
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        }, pageable);
    }

    @Override
    public Page<JwQuestionUserEntity> findAll(Pageable pageable, String name, String schoolId) {
        return this.findAll(new Specification<JwQuestionUserEntity>() {
            @Override
            public Predicate toPredicate(Root<JwQuestionUserEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> pathName = root.get("name");
                Path<String> pathSchoolId = root.get("schoolId");
                ArrayList<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmptyOrWhitespace(name)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathName,"%"+ name +"%")));
                }
                if (!StringUtils.isEmptyOrWhitespace(schoolId)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathSchoolId,schoolId)));
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        }, pageable);
    }

    @Override
    public List<JwQuestionUserEntity> findAllByQuestionIdOrderByCreateTimeAsc(Pageable pageable, String questionId) {
        return this.repository.findAllByQuestionIdOrderByCreateTimeAsc(pageable,questionId);
    }

    @Override
    public Long selectCountByQuestionId(String questionId) {
        return this.repository.selectCountByQuestionId(questionId);
    }
}